package logic

import (
	define2 "MyBlogv2/blog-common/define"
	"MyBlogv2/blog-common/helper"
	"MyBlogv2/blog-common/models"
	"context"
	"github.com/jinzhu/copier"
	"strconv"

	"MyBlogv2/Servers/Article/article"
	"MyBlogv2/Servers/Article/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetArticleDetailByIdLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetArticleDetailByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetArticleDetailByIdLogic {
	return &GetArticleDetailByIdLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetArticleDetailByIdLogic) GetArticleDetailById(in *Article.ArticleRequest) (*Article.ArticleDetailVoResponse, error) {
	id := strconv.FormatInt(in.Id, 10)
	var articleVo models.ArticleVo
	var articleDetailVo models.ArticleDetailVo
	models.Db.Model(new(models.ArticleVo)).
		Table("t_article a").
		Select("a.*,c.category_name as category_name").
		Joins("left join t_category c on a.category_id = c.id").
		Where("a.id = ?", id).
		Where("a.status = ?", define2.PUBLIC_ARTICLE).
		First(&articleVo)
	var tagsVo []models.TagVo
	var tags []models.Tag
	models.Db.Model(new(models.Tag)).
		Table("t_tag t").
		Joins("left join t_article_tag at on at.tag_id = t.id").
		Where("at.article_id = ?", id).
		Distinct("t.tag_name").
		Find(&tags)
	copier.Copy(&tagsVo, &tags)
	copier.Copy(&articleDetailVo, &articleVo)
	articleDetailVo.Tags = tagsVo
	var nextArticle models.Article
	var preArticle models.Article
	models.Db.Model(new(models.Article)).Where("id > ?", id).Order("id asc").First(&nextArticle)
	models.Db.Model(new(models.Article)).Where("id < ?", id).Order("id desc").First(&preArticle)
	var likeCount int64
	models.Db.Model(new(models.Like)).Where("type_id = ? and type = ?", id, define2.LIKE_TYPE_ARTICLE).Count(&likeCount)
	var favoriteCount int64
	models.Db.Model(new(models.Favorite)).Where("type_id = ? and type = ?", id, define2.LIKE_TYPE_ARTICLE).Count(&favoriteCount)
	var commentCount int64
	models.Db.Model(new(models.Comment)).Where("type_id = ? and type = ?", id, define2.LIKE_TYPE_ARTICLE).Count(&commentCount)
	articleDetailVo.CommontCount = int(commentCount)
	articleDetailVo.FavoriteCount = int(favoriteCount)
	articleDetailVo.LikeCount = int(likeCount)
	articleDetailVo.NextArticleId = nextArticle.Id
	articleDetailVo.PreArticleId = preArticle.Id
	articleDetailVo.NextArticleTitle = nextArticle.ArticleTitle
	articleDetailVo.PreArticleTitle = preArticle.ArticleTitle

	var articleDetailVoResponse Article.ArticleDetailVoResponse
	copier.Copy(&articleDetailVoResponse, &articleDetailVo)
	articleDetailVoResponse.CreateTime = helper.MyTimeToTimestamp(articleDetailVo.CreateTime)
	articleDetailVoResponse.UpdateTime = helper.MyTimeToTimestamp(articleDetailVo.UpdateTime)
	for i, v := range articleDetailVo.Tags {
		copier.Copy(&articleDetailVoResponse.Tags[i], &v)
		articleDetailVoResponse.Tags[i].CreateTime = helper.MyTimeToTimestamp(v.CreateTime)
		articleDetailVoResponse.Tags[i].UpdateTime = helper.MyTimeToTimestamp(v.UpdateTime)
	}

	return &articleDetailVoResponse, nil
}
